<template>
<el-container>
    <el-header height="30%">
        <div style="border-radius: 4px;" class="page-container">
            <el-tabs v-model="activeName" type="card">
                <el-tab-pane label="基本信息" name="activeName">
                    <el-form :inline="true" :model="purchaseOrderForm" ref="purchaseOrderForm" :rules="purchaseOrderFormRules">
                        <div style="height:30%;width:90%">
                            <el-row :gutter="24">
                                <el-col :span="8" style="text-align:right">
                                    <el-form-item label="来源类型" prop="fromType">
                                        <el-select placeholder="请选择" v-model="purchaseOrderForm.fromType" size="mini" style="width:200px">
                                            <el-option label="手动录入" :value="0"></el-option>
                                            <el-option label="直通转入" :value="1"></el-option>
                                            <el-option label="直送转入" :value="2"></el-option>
                                            <el-option label="自动补货" :value="2"></el-option>
                                            <el-option label="无采购单收货" :value="2"></el-option>
                                            <el-option label="内部销售订单" :value="2"></el-option>
                                            <el-option label="要货补货" :value="2"></el-option>
                                            <el-option label="采购计划" :value="2"></el-option>
                                        </el-select>
                                    </el-form-item>
                                </el-col>
                                <el-col :span="8" style="text-align:right">
                                    <el-form-item label="供应商" prop="supplierName">
                                        <el-input size="mini" readonly="readonly" v-model="purchaseOrderForm.supplierName" @focus="showSupplier" style="width: 200px;" c;></el-input>
                                    </el-form-item>
                                </el-col>
                            </el-row>

                            <el-row :gutter="24">
                                <el-col :span="8" style="text-align:right">
                                    <el-form-item label="发货联系人" prop="supplierContactsName" :show-message="false" :required="true">
                                        <el-input size="mini" readonly @focus="showContact" v-model="purchaseOrderForm.supplierContactsName" style="width: 200px;"></el-input>
                                    </el-form-item>
                                </el-col>

                                <el-col :span="8" style="text-align:right">
                                    <el-form-item label="交易币别" prop="transactionCurrency">
                                        <el-select v-model="purchaseOrderForm.transactionCurrency" size="mini" style="width:200px">
                                            <el-option label="CNY" :value="1"></el-option>
                                            <el-option label="AUD" :value="2"></el-option>
                                            <el-option label="USD" :value="3"></el-option>
                                            <el-option label="GBP" :value="4"></el-option>
                                        </el-select>
                                    </el-form-item>
                                </el-col>
                            </el-row>
                            <el-row :gutter="24">
                                <el-col :span="8" style="text-align:right">
                                    <el-form-item label="预计到货日期" prop="purchaseTime">
                                        <el-date-picker v-model="purchaseOrderForm.deliveryTime" size="mini" type="datetime" style="width: 200px;" placeholder="选择日期">
                                        </el-date-picker>
                                    </el-form-item>
                                </el-col>

                                <el-col :span="8" style="text-align:right">
                                    <el-form-item label="收货商家" prop="merchantName">
                                        <el-input size="mini" v-model="purchaseOrderForm.merchantName" disabled style="width: 200px;"></el-input>
                                    </el-form-item>
                                </el-col>

                                <el-col :span="8" style="text-align:right">
                                    <el-form-item label="收货店铺" prop="storeName">
                                        <el-input size="mini" v-model="purchaseOrderForm.storeName" disabled style="width: 200px;"></el-input>
                                    </el-form-item>
                                </el-col>
                            </el-row>

                            <el-row :gutter="24">

                                <el-col :span="8" style="text-align:right">
                                    <el-form-item label="收货仓库" prop="warehouseId">
                                        <el-select v-model="purchaseOrderForm.warehouseId" placeholder="请选择" size="mini" style="width:200px">
                                            <el-option v-for="item in warehouseList" :key="item.id" :label="item.warehouseName" :value="item.id">
                                            </el-option>
                                        </el-select>
                                    </el-form-item>
                                </el-col>

                                <el-col :span="8" style="text-align:right">
                                    <el-form-item label="收货方式" prop="receiveMethod">
                                        <el-select placeholder="请选择" v-model="purchaseOrderForm.receiveMethod" size="mini" style="width:200px">
                                            <el-option label="接受-入库" :value="0"></el-option>
                                            <el-option label="直接-入库" :value="1"></el-option>
                                            <el-option label="接受-检验-入库" :value="2"></el-option>
                                        </el-select>
                                    </el-form-item>
                                </el-col>

                                <el-col :span="8" style="text-align:right">
                                    <el-form-item label="配送方式" prop="deliveryMethod">
                                        <el-select placeholder="请选择" v-model="purchaseOrderForm.deliveryMethod" size="mini" style="width:200px">
                                            <el-option label="快递物流" :value="0"></el-option>
                                        </el-select>
                                    </el-form-item>
                                </el-col>
                            </el-row>

                            <el-row :gutter="24">

                                <el-col :span="8" style="text-align:right">
                                    <el-form-item label="状态" prop="status">
                                        <el-select disabled v-model="purchaseOrderForm.status" size="mini" style="width:200px">
                                            <el-option label="待提交" :value="0"></el-option>
                                            <el-option label="待审核" :value="1"></el-option>
                                            <el-option label="已审核" :value="2"></el-option>
                                            <el-option label="审核拒绝" :value="3"></el-option>
                                            <el-option label="已发货" :value="4"></el-option>
                                            <el-option label="已结案" :value="5"></el-option>
                                            <el-option label="已过期" :value="6"></el-option>
                                            <el-option label="已作废" :value="7"></el-option>
                                        </el-select>
                                    </el-form-item>
                                </el-col>

                                <el-col :span="8" style="text-align:right">
                                    <el-form-item label="采购履行状态" prop="fulfillStatus">
                                        <el-select :disabled="true" v-model="purchaseOrderForm.fulfillStatus" size="mini" style="width:200px">
                                            <el-option label="未履行" :value="1"></el-option>
                                            <el-option label="部分履行" :value="2"></el-option>
                                            <el-option label="完全履行" :value="3"></el-option>
                                        </el-select>
                                    </el-form-item>
                                </el-col>

                                <el-col :span="8" style="text-align:right">
                                    <el-form-item label="联系人名称">
                                        <el-input size="mini" style="width: 200px;"></el-input>
                                    </el-form-item>
                                </el-col>
                            </el-row>

                            <el-row :gutter="24">
                                <el-col :span="8" style="text-align:right">
                                    <el-form-item label="备注">
                                        <el-input size="mini" type="textarea" style="width:200px"></el-input>
                                    </el-form-item>
                                </el-col>
                            </el-row>

                            <el-row :gutter="24">
                                <el-col :span="8" style="text-align:right">
                                    <el-form-item label="收货地址" prop="receiveAddress">
                                        <el-input v-model="purchaseOrderForm.receiveAddress" placeholder="省/市/区/地址" size="mini" style="width:200px"></el-input>
                                    </el-form-item>
                                </el-col>
                                <el-col :span="4">
                                    <div class="grid-content" style="text-align:right">
                                        <label>附件</label>
                                    </div>
                                </el-col>
                                <el-col :span="4">
                                    <div class="grid-content" style="text-align:left">
                                        <el-upload class="upload-demo" action="https://jsonplaceholder.typicode.com/posts/" multiple :limit="3">
                                            <el-button size="small" type="primary">点击上传</el-button>
                                            <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>
                                        </el-upload>
                                    </div>
                                </el-col>
                            </el-row>
                        </div>
                    </el-form>
                </el-tab-pane>
            </el-tabs>
        </div>
    </el-header>
    <el-main>
        <div>
            <el-tabs>
                <el-tab-pane label="商品明细">
                    <div style="text-align:left">
                        <kt-button label="按合同添加商品" type="primary" @click="choseOrder" />
                        <!-- <kt-button label="删除" type="primary" @click="delOrder" />
              <kt-button label="下载导入模板" type="primary" @click="dowOrder" />
              <kt-button label="导入采购单" type="primary" @click="importOrder" />-->
                    </div>
                    <div>
                        <el-table :data="tableData" border style="width: 100%">
                            <el-table-column prop="checks" type="selection"></el-table-column>
                            <el-table-column prop="productCode" label="商品编码" width="180"></el-table-column>
                            <el-table-column prop="productName" label="商品名称" width="180"></el-table-column>
                            <el-table-column prop="productSize" label="商品规格" width="180"></el-table-column>
                            <el-table-column prop="productBarNo" label="商品条码" width="180"></el-table-column>
                            <!-- <el-table-column prop="shoptype" label="商品类目" width="180"></el-table-column> -->
                            <!-- <el-table-column prop="shopBrand" label="商品品牌" width="180"></el-table-column> -->
                            <el-table-column prop="purchaseNum" label="本次采数量" width="180"></el-table-column>
                            <!-- <el-table-column prop="merchantCode" label="采购单位" width="180"></el-table-column> -->
                            <!-- <el-table-column prop="purchasenums" label="* 退货仓库	" width="180"></el-table-column>
                <el-table-column prop="regularnums" label="可退数量" width="180"></el-table-column>-->
                            <el-table-column prop="realDeliveryNum" label="发货数量" width="180"></el-table-column>
                            <el-table-column prop="editRealDeliveryNum" label="本次发货数量" width="180">
                                <template slot-scope="scope">
                                    <el-input :disabled="editType!=1" size="mini" v-model="scope.row.editRealDeliveryNum" @change="change(scope.$index, scope.row,1)"></el-input>
                                </template>
                            </el-table-column>
                            <el-table-column prop="realRecievedNum" label="收货数量" width="180"></el-table-column>
                            <el-table-column prop="editRealRecievedNum" label="本次收货数量" width="180">
                                <template slot-scope="scope">
                                    <el-input :disabled="editType!=2" size="mini" v-model="scope.row.editRealRecievedNum" @change="change(scope.$index,scope.row,2)"></el-input>
                                </template>
                            </el-table-column>
                            <el-table-column prop="tradeCurrency" label="交易币别" width="180"></el-table-column>
                            <el-table-column prop="exchangeRate" label="汇率" width="180"></el-table-column>
                            <el-table-column prop="originAmountWithTax" label="含税采购价" width="180"></el-table-column>
                            <el-table-column prop="taxRate" label="税率(%)	" width="180"></el-table-column>
                            <el-table-column prop="amountWithoutTax" label="未税采购价" width="180"></el-table-column>
                            <el-table-column prop="amountWithTax" label="含税金额" width="180"></el-table-column>
                            <!-- <el-table-column prop="purchaseprice" label="税额" width="180"></el-table-column> -->
                            <el-table-column prop="amountWithoutTax" label="未税金额" width="180"></el-table-column>
                            <!-- <el-table-column prop="tax" label="本币未税成本价" width="180"></el-table-column> -->
                            <!-- <el-table-column prop="amounttax" label="本币含税成本价" width="180"></el-table-column> -->
                            <!-- <el-table-column prop="receiptmethod" label="本币含税金额" width="180"></el-table-column> -->
                            <!-- <el-table-column prop="remark" label="本币税额" width="180"></el-table-column> -->
                            <!-- <el-table-column prop="warehousepurchase" label="本币未税金额" width="180"></el-table-column> -->
                            <!-- <el-table-column prop="code" label="采购单号" width="180"></el-table-column> -->
                            <!-- <el-table-column prop="purchasealone" label="收货单号" width="180"></el-table-column> -->
                            <!-- <el-table-column prop="asnno" label="合同属性" width="180"></el-table-column> -->
                            <!-- <el-table-column prop="asnline" label="已开票数量" width="180"></el-table-column> -->
                            <el-table-column prop="contractCode" label="合同编码" width="180"></el-table-column>
                            <!-- <el-table-column prop="contracttype" label="合同类型" width="180"></el-table-column> -->
                            <!-- <el-table-column prop="attributes" label="合同属性" width="180"></el-table-column> -->
                        </el-table>
                    </div>
                    <!-- <div style="margin-left:70%">
              <el-row :gutter="14">
                <el-col :span="7">
                  <div class="grid-content" style="text-align:right">
                    <label>合计平品项数</label>
                  </div>
                </el-col>
                <el-col :span="7">
                  <div class="grid-content" style="text-align:right;width:100%">
                    <span>23</span>
                  </div>
                </el-col>
              </el-row>
              <el-row :gutter="14">
                <el-col :span="7">
                  <div class="grid-content" style="text-align:right">
                    <label>合计含税金额</label>
                  </div>
                </el-col>
                <el-col :span="7">
                  <div class="grid-content" style="text-align:right;width:100%">
                    <span>5952.00</span>
                  </div>
                </el-col>
              </el-row>
              <el-row :gutter="14">
                <el-col :span="7">
                  <div class="grid-content" style="text-align:right">
                    <label>合计未税金额</label>
                  </div>
                </el-col>
                <el-col :span="7">
                  <div class="grid-content" style="text-align:right;width:100%">
                    <span>451.00</span>
                  </div>
                </el-col>
              </el-row>
            </div>-->
                </el-tab-pane>
                <el-tab-pane label="操作日志">
                    <div>
                        <el-table :data="tableLog" border style="width: 100%">
                            <el-table-column prop="operatingid" label="操作用户ID"></el-table-column>
                            <el-table-column prop="operator" label="操作人"></el-table-column>
                            <el-table-column prop="operating_time" label="操作时间"></el-table-column>
                            <el-table-column prop="operating" label="操作"></el-table-column>
                            <el-table-column prop="remark" label="备注"></el-table-column>
                        </el-table>
                    </div>
                </el-tab-pane>
            </el-tabs>
        </div>
    </el-main>
    <el-row style="text-align:center">
        <kt-button label="保存基本信息" type="primary" @click="savePurchaseOrder" />
        <kt-button label="关 闭" type="primary" @click="closePage" />
        <!-- <kt-button label="保存" type="primary" @click="save" />
      <kt-button label="提交" type="primary" @click="saves" /> -->
    </el-row>

    <!-- 选择店铺 -->
    <el-dialog title="选择店铺" :visible.sync="showModel" width="45%" height="100%" label-width="80px" label-position="right">
        <el-form style="text-align:left" label-width="80px" label-position="right">
            <el-row>
                <div style="text-align:center">
                    <el-form-item label="店铺名称" style="float:left;margin-left:90px">
                        <el-input auto-complete="off" width="20%"></el-input>
                    </el-form-item>

                    <el-form-item label="店铺编码" style="float:left;margin-left:90px">
                        <el-input auto-complete="off" width="20%"></el-input>
                    </el-form-item>
                </div>
            </el-row>

            <div style="text-align:center">
                <el-form-item>
                    <kt-button icon="fa fa-search" :label="$t('action.search')" type="primary" @click="findPage(null)" />
                    <kt-button label="重置" type="primary" />
                </el-form-item>
            </div>

            <el-table :data="shopTable" border style="width: 100%" :visible.sync="showTable">
                <el-table-column prop="checks" type="selection"></el-table-column>
                <el-table-column prop="name" label="商家名称" width="280"></el-table-column>
                <el-table-column prop="number" label="店铺编码" width="280"></el-table-column>
                <el-table-column prop="shopname" label="店铺名称"></el-table-column>
                <el-table-column prop="type" label="店铺类型"></el-table-column>
            </el-table>
        </el-form>
        <div slot="footer" class="dialog-footer" style="text-align:right">
            <el-button @click="cancels">取 消</el-button>
        </div>
    </el-dialog>

    <!-- 发货联系人 -->
    <el-dialog title="请输入联系人" :visible.sync="showContactModel" width="45%" height="100%" label-width="80px" label-position="right">
        <el-table :data="contactTable" border style="width: 100%" highlight-current-row @current-change="contactHandleCurrentChange">
            <el-table-column prop="contactsName" label="联系人名称"></el-table-column>
            <el-table-column prop="sex" label="联系人名称">
                <template slot-scope="scope">
                    <span v-if="scope.row.sex==1">男</span>
                    <span v-else-if="scope.row.sex==2">女</span>
                    <span v-else>未知</span>
                </template>
            </el-table-column>
            <el-table-column prop="contactsPosition" label="联系人职位" width="280"></el-table-column>
            <el-table-column prop="mobile" label="联系人手机"></el-table-column>
            <el-table-column prop="egularPhone" label="固定电话"></el-table-column>
            <el-table-column prop="faxNumber" label="传真号码" width="280"></el-table-column>
            <el-table-column prop="qq" label="QQ"></el-table-column>
            <el-table-column prop="wechat" label="微信"></el-table-column>
            <el-table-column prop="email" label="联系邮箱" width="280"></el-table-column>
            <el-table-column prop="address" label="详细地址"></el-table-column>
            <el-table-column prop="contactsProvinceName" label="省"></el-table-column>
            <el-table-column prop="contactsCityName" label="市"></el-table-column>
            <el-table-column prop="contactsRegionName" label="区/县" width="280"></el-table-column>
        </el-table>
        <div slot="footer" class="dialog-footer" style="text-align:right">
            <el-button @click="clearContact">取 消</el-button>
        </div>
    </el-dialog>

    <!-- 选择供应商 -->

    <el-dialog title="选择供应商123" :visible.sync="showSupplierModel" width="45%" height="100%" label-width="80px" label-position="right">
        <el-form style="text-align:left" label-width="80px" label-position="right">
            <el-row>
                <div style="text-align:center">
                    <el-form-item label="供应商编码" style="float:left;margin-left:100px">
                        <el-input v-model="supplierSearchForm.supplierCode" auto-complete="off" width="30%"></el-input>
                    </el-form-item>

                    <el-form-item label="供应商名称" style="float:left;margin-left:100px">
                        <el-input v-model="supplierSearchForm.supplierName" auto-complete="off" width="30%"></el-input>
                    </el-form-item>
                </div>
            </el-row>

            <div style="text-align:center">
                <el-form-item>
                    <kt-button icon="fa fa-search" :label="$t('action.search')" type="primary" @click="findSupplierPage(null)" />
                    <kt-button icon="fa fa-search" label="重置" type="primary" />
                </el-form-item>
            </div>

            <el-table :data="supplierTable" border style="width: 100%" :visible.sync="showSupplierTable" highlight-current-row @current-change="supplierHandleCurrentChange">
                <el-table-column prop="supplierGlnCode" label="123"></el-table-column>
                <el-table-column prop="supplierName" label="供应商名称"></el-table-column>
            </el-table>
        </el-form>
        <div slot="footer" class="dialog-footer" style="text-align:right">
            <el-button @click="cancelSupplier">取 消</el-button>
        </div>
    </el-dialog>

    <!-- 选择合同商品 -->
    <div style="overflow-x:visible">
        <el-dialog title="选择合同商品" :visible.sync="choseModel" width="45%" height="100%" label-width="80px" label-position="right">
            <el-form style="text-align:left" label-width="80px" label-position="right">
                <el-row>
                    <div style="text-align:center">
                        <el-form-item label="合同编码" style="float:left;margin-left:90px">
                            <el-input auto-complete="off" v-model="filter.contractCode" width="20%" size="mini"></el-input>
                        </el-form-item>

                        <!-- <el-form-item label="店铺编码" style="float:left;margin-left:90px">
                <el-input auto-complete="off" width="20%" size="mini"></el-input>
              </el-form-item>-->
                    </div>
                </el-row>
                <div style="text-align:center">
                    <el-form-item>
                        <kt-button icon="fa fa-search" :label="$t('action.search')" type="primary" @click="findPage(null)" />
                        <kt-button label="重置" type="primary" />
                    </el-form-item>
                </div>

                <el-table :data="proTables" border style="width: 100%" :visible.sync="choseTable" @selection-change="selectionPurchaseContractProduct">
                    <el-table-column type="selection" width="55"></el-table-column>
                    <el-table-column prop="contractCode" label="合同编码"></el-table-column>
                    <el-table-column prop="contractCode" label="合同编码"></el-table-column>
                    <!-- <el-table-column prop="recivenumber" label="合同类型"></el-table-column> -->
                    <el-table-column prop="ordertype" label="店铺名称"></el-table-column>
                    <el-table-column prop="shopname" label="店铺编码"></el-table-column>
                    <el-table-column prop="supplierName" label="供应商名称"></el-table-column>
                    <el-table-column prop="supplierCode" label="供应商编码"></el-table-column>
                    <el-table-column prop="productbarno" label="商品编码"></el-table-column>
                    <el-table-column prop="productName" label="商品名称"></el-table-column>
                    <!-- <el-table-column prop="shopname" label="主计量单位"></el-table-column> -->
                    <el-table-column prop="productUnit" label="采购单位"></el-table-column>
                    <el-table-column prop="transferRate" label="转换率"></el-table-column>
                    <el-table-column prop="productSize" label="商品规格"></el-table-column>
                    <el-table-column prop="originAmountWithTax" label="含税采购价"></el-table-column>
                    <el-table-column prop="originTaxRate" label="进项税率"></el-table-column>
                    <el-table-column prop="ordertype" label="流转途径"></el-table-column>
                    <el-table-column prop="minNum" label="起订量"></el-table-column>
                    <el-table-column prop="address" label="订货倍数"></el-table-column>
                    <el-table-column prop="originTransportType" label="运输方式"></el-table-column>
                    <el-table-column prop="originHasTransportPrice" label="是否含运费"></el-table-column>
                    <!-- <el-table-column prop="create_time" label="库存组织进目录"></el-table-column>
            <el-table-column prop="create_time" label="库存量"></el-table-column>-->
                </el-table>
            </el-form>
            <div slot="footer" class="dialog-footer" style="text-align:right">
                <el-button @click="addContractIdByPurchaseOrder">确定</el-button>
            </div>
        </el-dialog>
    </div>
</el-container>
</template>

<script>
import CyTable from "@/views/Core/CyTable";
import KtButton from "@/views/Core/KtButton";
export default {
    components: {
        CyTable,
        KtButton
    },
    data() {
        return {
            proTables: [],
            filter: {
                contractCode: null
            },
            recive: 0,
            send: 0,
            id: 1,
            //联系人
            showContactModel: false,
            showContactTable: false,
            choseModel: false,
            choseTable: false,
            showSupplierModel: false,
            showSupplierTable: false,
            showModel: false,
            showTable: false,
            showModel1: false,
            showTable1: false,
            activeName: "activeName",
            contractCode: null,
            arr: {},
            editarr: [],
            editsarr: [],
            //联系人
            contactTable: [],
            //收货单
            choseTabled: [{
                order: "A102315"
            }],
            tableLog: [],
            tableData: [],
            purchaseOrderForm: {
                merchantId: "",
                merchantName: "",
                storeId: "",
                storeName: "",
                supplierId: "",
                supplierName: "",
                supplierContactsId: "",
                supplierContactsName: "",
                transactionCurrency: "",
                warehouseId: "",
                warehouseName: "",
                purchaseUserName: "",
                fromType: 1,
                receiveMethod: "",
                deliveryMethod: "",
                sendProvinceId: "",
                sendCityId: "",
                sendAreaId: "",
                sendAddress: "",
                receiveProvinceId: "",
                receiveCityId: "",
                receiveAreaId: "",
                receiveAddress: "",
                purchaseTime: "",
                deliveryTime: "",
                createTime: "",
                status: 1,
                fulfillStatus: 1
            },
            purchaseOrderFormRules: {
                region: [{
                    required: true,
                    message: "请选择来源类型",
                    trigger: "change"
                }],
                supplierName: [{
                    required: true,
                    message: "请选择供应商",
                    trigger: "change"
                }],
                purchaseTime: [{
                    required: true,
                    message: "请选择采购日期",
                    trigger: "change"
                }],
                deliveryTime: [{
                    required: true,
                    message: "请选择预计到货日期",
                    trigger: "change"
                }],
                warehouseId: [{
                    required: true,
                    message: "请选择收货仓库",
                    trigger: "change"
                }],
                receiveMethod: [{
                    required: true,
                    message: "请选择收货方式",
                    trigger: "change"
                }],
                deliveryMethod: [{
                    required: true,
                    message: "请输入配送方式",
                    trigger: "change"
                }],
                sendAddress: [{
                    required: true,
                    message: "请输入发货地址",
                    trigger: "blur"
                }],
                receiveAddress: [{
                    required: true,
                    message: "请输入收货地址",
                    trigger: "blur"
                }]
            },
            shopTable: [],

            warehouseList: [],
            //供应商
            supplierTable: [],
            supplierSearchForm: {},
            supplierCurrentRow: null,
            purchaseOrderId: "",
            productSelection: [],
            editType: ""
            //
        };
    },
    methods: {
        // 根据合同编码获得对应商品信息
        findPage: function (data) {
            if (this.filter.contractCode == null) {
                this.$message({
                    message: "请输入合同编号！",
                    type: "warning"
                });
                return;
            }
            let this_ = this;
            this.utils.request.findPurchaseContractProduct(this.filter, function (
                res
            ) {
                this_.proTables = res.data;
                console.log(this_.proTables);
            });
        }, //根据合同生成对应采购单

        //分页查询供应商列表 updated by tzc at 2020-10-16 15:19:32
        findSupplierPage: function (data) {
            debugger;
            let this_ = this;
            this.utils.request.supplierInformationList(this.filter, function (res) {
                this_.proTables = res.data;
                console.log(this_.proTables);
            });
        }, //根据合同生成对应采购单

        addOrder() {
            let this_ = this;
            (this.proModel = false), (this.proTable = false);
            //判断合同内商品数据是否为空
            if (this.proTables.length > 0) {
                this.utils.request.generatePurchaseOrder({
                        contractNo: this.proTables[0].contractCode
                    },
                    function (res) {
                        if (res.code == "0000") {
                            this_.$message({
                                message: "操作成功",
                                type: "success"
                            });
                            this_.choseModel = false;
                            this_.choseTable = false;
                            //跳转到采购单页面
                            this_.$router.push("/PurchaseOrder");
                        } else {
                            this_.$message({
                                message: "操作失败, " + res.msg,
                                type: "error"
                            });
                            this_.$router.push("/PurchaseOrder");
                            this_.choseModel = false;
                            this_.choseTable = false;
                        }
                    }
                );
            } else {
                this.$message({
                    message: "合同内商品数据不能为空！",
                    type: "error"
                });
            }
        },
        closePage() {
            this.$router.push("/PurchaseOrder");
        },
        change(index, e, editType) {
            let that = this;
            if (editType == 1) {
                if (
                    Number(e.editRealDeliveryNum) + Number(e.realDeliveryNum) >
                    Number(e.purchaseNum)
                ) {
                    that.$message({
                        message: "总发货数量不能大于采购数量",
                        type: "error"
                    });
                    return;
                }
                let params = Object.assign({}, e);
                params.editType = editType;
                params.editNum = e.editRealDeliveryNum;
                that.utils.request.editPurchaseOrderProductNum(params, res => {
                    if (res.code == "0000") {
                        that.getPurchaseOrderProductList();
                        that.$message({
                            message: "操作",
                            type: "success"
                        });
                    }
                });
            } else {
                if (
                    Number(e.editRealRecievedNum) + Number(e.realRecievedNum) >
                    Number(e.realDeliveryNum)
                ) {
                    that.$message({
                        message: "总收货数量不能大于发货数量",
                        type: "error"
                    });
                    return;
                }
                let params = Object.assign({}, e);
                params.editType = editType;
                params.editNum = e.editRealRecievedNum;
                that.utils.request.editPurchaseOrderProductNum(params, res => {
                    if (res.code == "0000") {
                        that.$message({
                            message: "操作",
                            type: "success"
                        });
                        that.getPurchaseOrderProductList();
                    }
                });
            }
        },
        getDetail() {
            let this_ = this;
            if (
                this.$route.params.id != null &&
                this.$route.params.id != "" &&
                this.$route.params.id != "null" &&
                this.$route.params.id != "undefined"
            ) {
                let params = {};
                params.id = this.$route.params.id;
                this.utils.request.getPurchaseOrderDetail(params, function (res) {
                    if (res.code === "0000") {
                        this_.form = res.data;
                        this_.contractCode = res.data.contractNo;
                        let paramss = {};
                        paramss.contractCode = this_.contractCode;
                        this_.getPurchaseOrderProductList();
                    } else {
                        this_.$message({
                            message: "操作失败, " + data.msg,
                            type: "error"
                        });
                    }
                });
                //
            }
        },

        print: function () {
            this.$message({
                message: "打印成功",
                type: "success"
            });
        },
        //删除
        delOrder() {
            this.$message({
                message: "删除成功",
                type: "success"
            });
        },
        //下载
        dowOrder() {
            this.$message({
                message: "下载成功",
                type: "success"
            });
        },
        //导入
        importOrder() {
            this.$message({
                message: "导入成功",
                type: "success"
            });
        },
        //保存
        save() {
            let this_ = this;
            var msg = this_.id == 1 ? "收货成功" : "发货成功";
            if (this_.editarr.length < 1 && this_.editsarr.length < 1) {
                this_.$message({
                    message: msg,
                    type: "success"
                });
                this_.findre();
                this_.$router.go(-1);
                return;
            }
            if (this_.id == 1) {
                this_.utils.request.savePurchaseContractProductRel({
                        arrs: this_.editarr
                    },
                    function (res) {
                        if (res.code === "0000") {
                            this_.$message({
                                message: msg,
                                type: "success"
                            });
                            this_.findPage();
                        } else {
                            this_.$message({
                                message: "操作失败",
                                type: "error"
                            });
                        }
                    }
                );
            } else {
                this_.utils.request.savePurchaseContractProductRel({
                        arr: this_.editsarr
                    },
                    function (res) {
                        if (res.code === "0000") {
                            this_.$message({
                                message: msg,
                                type: "success"
                            });
                        } else {
                            this_.$message({
                                message: "操作失败",
                                type: "error"
                            });
                        }
                    }
                );
            }
            this_.findre();
            this_.$router.go(-1);
        },
        updataPurchaseOrderStatus(status) {
            let this_ = this;
            var nowdate = new Date();
            if (nowdate.getTime() > new Date(this_.form.endTime).getTime()) {
                status = 6;
            }
            this_.utils.request.savePurchaseOrder({
                    status: status,
                    id: this.$route.params.id
                },
                function (res) {
                    // if (res.code === "0000") {
                    //     this_.$message({ message: msg, type: "success" });
                    // } else {
                    //     this_.$message({message: "操作失败", type: "error"});
                    // }
                }
            );
        },
        findre() {
            let this_ = this;
            this_.utils.request.findPurchaseContractProductRelListPage({
                    contractCode: this_.form.contractNo
                },
                function (res) {
                    if (res.code === "0000") {
                        let status = 0;
                        for (var i = 0; i < res.data.list.length; i++) {
                            if (
                                res.data.list[i].realRecievedNum <
                                res.data.list[i].purchaseNum ||
                                res.data.list[i].realDeliveryNum < res.data.list[i].purchaseNum
                            ) {
                                status = 0;
                                break;
                            }
                            if (
                                res.data.list[i].realRecievedNum ==
                                res.data.list[i].purchaseNum &&
                                res.data.list[i].realDeliveryNum == res.data.list[i].purchaseNum
                            ) {
                                status = 5;
                                continue;
                            }
                        }
                        // this_.updataPurchaseOrderStatus(status);
                    } else {
                        this_.$message({
                            message: "操作失败",
                            type: "error"
                        });
                    }
                }
            );
        },
        //提交
        saves() {
            let flag = true;
            //判断收货数量
            this.tableData.forEach(function (value, i) {
                if (
                    value.realRecievedNum > value.purchaseNum ||
                    value.realDeliveryNum > value.purchaseNum
                ) {
                    flag = false;
                }
            });
            if (!flag) {
                this.$message({
                    message: "收货数量或发货数量不能大于采购数量",
                    type: "error"
                });
                return;
            }

            let this_ = this;
            var msg = this_.id == 1 ? "收货成功" : "发货成功";
            if (this_.editarr.length < 1 && this_.editsarr.length < 1) {
                this_.$message({
                    message: msg,
                    type: "success"
                });
                this_.findre();
                this_.$router.go(-1);
                return;
            }
            if (this_.id == 1) {
                this_.utils.request.savePurchaseContractProductRel({
                        arrs: this_.editarr
                    },
                    function (res) {
                        if (res.code === "0000") {
                            this_.$message({
                                message: msg,
                                type: "success"
                            });
                        } else {
                            this_.$message({
                                message: "操作失败",
                                type: "error"
                            });
                        }
                    }
                );
            } else {
                this_.utils.request.savePurchaseContractProductRel({
                        arr: this_.editsarr
                    },
                    function (res) {
                        if (res.code === "0000") {
                            this_.$message({
                                message: msg,
                                type: "success"
                            });
                        } else {
                            this_.$message({
                                message: "操作失败",
                                type: "error"
                            });
                        }
                    }
                );
            }
            this_.findre();
            this_.$router.go(-1);
        },
        //联系人
        showContact() {
            if (
                this.purchaseOrderForm.supplierId == "" ||
                this.purchaseOrderForm.supplierId == null
            ) {
                this.$message({
                    message: "请先选择供应商",
                    type: "warning"
                });
                return false;
            }
            this.getSupplierContactsList();
            this.showContactModel = true;
            this.showContactTable = true;
        },
        //收货单table
        choseOrder: function () {
            if (this.purchaseOrderId == "" || this.purchaseOrderId == null) {
                this.$message({
                    message: "请先保存采购单基本信息",
                    type: "warning"
                });
                return false;
            }
            this.choseModel = true;
            this.choseTable = true;
        },
        //店铺table
        showStore() {
            this.showModel = true;
            this.showTable = true;
        },
        //供应商
        showSupplier: function () {
            this.showSupplierModel = true;
            this.showSupplierTable = true;
        },

        //取消按钮
        cancels: function () {
            this.showModel = false;
            this.showTable = false;
        },
        //取消按钮
        cancels1: function () {
            this.showModel1 = false;
            this.showTable1 = false;
        },
        //取消按钮
        cancelSupplier: function () {
            this.showSupplierModel = false;
            this.showSupplierTable = false;
        },
        //取消按钮
        clearChose: function () {
            this.choseModel = false;
            this.choseTable = false;
        },
        //取消按钮
        clearContact: function () {
            this.showContactModel = false;
            this.showContactTable = false;
        },
        //查询供应商列表
        getSupplierInformationList: function () {
            let that = this;
            this.utils.request.supplierInformationListPage({}, res => {
                if (res.code == "0000") {
                    that.supplierTable = res.data.rows;
                } else {
                    that.supplierTable = [];
                }
            });
        },
        //选择供应商
        supplierHandleCurrentChange(val) {
            this.purchaseOrderForm.supplierId = val.id;
            this.purchaseOrderForm.supplierName = val.supplierName;
            this.purchaseOrderForm.transactionCurrency = val.transactionCurrency;
            this.purchaseOrderForm.merchantId = val.merchantId;
            this.purchaseOrderForm.merchantName = val.merchantName;
            this.purchaseOrderForm.storeId = val.storeId;
            this.purchaseOrderForm.storeName = val.storeName;
            this.showSupplierModel = false;
            this.purchaseOrderForm.code = Date.parse(new Date());
        },
        //查询供应商联系人列表
        getSupplierContactsList() {
            let that = this;
            this.utils.request.getSupplierContactsList({}, res => {
                if (res.code == "0000") {
                    that.contactTable = res.data.list;
                } else {
                    that.contactTable = [];
                }
            });
        },
        //选择供应商联系人
        contactHandleCurrentChange(val) {
            this.$message({
                message: "已将发货联系人地址复制到了发货地址",
                type: "success"
            });
            this.purchaseOrderForm.supplierContactsId = val.id;
            this.purchaseOrderForm.supplierContactsName = val.contactsName;
            this.purchaseOrderForm.sendAddress = val.address;
            this.showContactModel = false;
        },
        //获取仓库列表
        getWarehouseList() {
            let that = this;
            this.utils.request.getWarehouseList({}, res => {
                if (res.code == "0000") {
                    that.warehouseList = res.data.rows;
                } else {
                    that.warehouseList = [];
                }
            });
        },
        //保存采购单基本信息
        savePurchaseOrder() {
            let that = this;
            this.$refs["purchaseOrderForm"].validate(valid => {
                if (valid) {
                    if (that.purchaseOrderId == "" || that.purchaseOrderId == null) {
                        this.utils.request.savePurchaseOrderInfo(
                            this.purchaseOrderForm,
                            res => {
                                if (res.code == "0000") {
                                    that.purchaseOrderId = res.data;
                                    that.$message({
                                        message: "保存成功",
                                        type: "success"
                                    });
                                } else {
                                    that.$message({
                                        message: "操作失败",
                                        type: "error"
                                    });
                                }
                            }
                        );
                    } else {
                        this.utils.request.updatePurchaseOrderInfo(
                            this.purchaseOrderForm,
                            res => {
                                if (res.code == "0000") {
                                    that.purchaseOrderId = res.data;
                                    that.$message({
                                        message: "编辑成功",
                                        type: "success"
                                    });
                                } else {
                                    that.$message({
                                        message: "操作失败",
                                        type: "error"
                                    });
                                }
                            }
                        );
                    }
                }
            });
        },
        getPurchaseOrder() {
            let that = this;
            if (this.purchaseOrderId != null && this.$route.params.id != "") {
                this.utils.request.getPurchaseOrderInfo({
                        id: this.$route.params.id
                    },
                    res => {
                        if (res.code == "0000") {
                            this.purchaseOrderForm = res.data;
                        } else {
                            that.$message({
                                message: "查询失败",
                                type: "error"
                            });
                        }
                    }
                );
            }
        },
        //选择合同商品
        selectionPurchaseContractProduct(val) {
            this.productSelection = val;
        },
        //保存采购单信息商品
        addContractIdByPurchaseOrder() {
            let that = this;
            if (this.filter.contractCode == null) {
                this.$message({
                    message: "请输入合同编号！",
                    type: "warning"
                });
                return;
            }
            if (that.productSelection.length == 0) {
                this.$message({
                    message: "请选择合同商品！",
                    type: "warning"
                });
                return;
            }
            console.log(this.productSelection);
            let params = {
                purchaseOrderId: that.purchaseOrderForm.id,
                purchaseOrderCode: that.purchaseOrderForm.code,
                contractCode: that.filter.contractCode,
                productList: that.productSelection
            };
            that.utils.request.savePurchaseOrderProduct(params, res => {
                if (res.code == "0000") {
                    that.$message({
                        message: "操作成功",
                        type: "success"
                    });
                    that.choseModel = false;
                    that.getPurchaseOrderProductList();
                } else {
                    that.$message({
                        message: res.msg,
                        type: "error"
                    });
                }
            });
        },
        //查询采购单下商品信息
        getPurchaseOrderProductList() {
            let that = this;
            let params = {
                purchaseOrderId: this.purchaseOrderId
            };
            this.utils.request.purchaseOrderProductQueryList(params, res => {
                if (res.code == "0000") {
                    that.tableData = res.data.rows;
                } else {
                    that.tableData = [];
                }
            });
        }
    },
    mounted() {
        this.getDetail();
        this.getPurchaseOrder();
        this.getSupplierInformationList();
        this.getWarehouseList();
        this.id = this.$route.params.id;
        this.purchaseOrderId = this.$route.params.id;
        this.editType = this.$route.params.type;
        // if(
        //     this.$route.params.id == null ||
        //     this.$route.params.id == "" ||
        //     this.$route.params.id == "null" ||
        //     this.$route.params.id == "undefined" ||
        //     this.$route.params.id == undefined){
        //   this.purchaseOrderForm.code =  Date.parse(new Date());
        //   console.log(this.purchaseOrderForm.code)
        // }
    }
};
</script>

<style scoped>
.el-row {
    margin-bottom: 15px;

    &:last-child {
        margin-bottom: 0;
    }
}

.el-col {
    border-radius: 4px;
}

.bg-purple-dark {
    background: #99a9bf;
}

.bg-purple {
    background: #d3dce6;
}

.bg-purple-light {
    background: #e5e9f2;
}

.grid-content {
    border-radius: 4px;
    min-height: 20px;
}

.row-bg {
    padding: 10px 0;
    background-color: #f9fafc;
}
</style>
